﻿

using System;
using System.ComponentModel;
using LitJson;
using UnityEngine;

namespace ETModel {
    public static class JsonHelper {
        public static string ToJson(object obj) {

            //return MongoHelper.ToJson(obj);//mongo的json序列化需要各种ID等限制
            //return JsonUtility.ToJson(obj);//unity自带的json序列化不支持默认的List和字典(要支持需要做改动的妥协)
            //return SimpleJson.SimpleJson.SerializeObject(obj);//这个序列化的字典是Key,Value的,需要Key:Value
            return JsonMapper.ToJson(obj);//之前litJson不支持float,现在已经支持
        }

        public static T FromJson<T>(string str) {
            /*
            T t = MongoHelper.FromJson<T>(str);
            ISupportInitialize iSupportInitialize = t as ISupportInitialize;
            if (iSupportInitialize == null) {
                return t;
            }
            iSupportInitialize.EndInit();
            return t;
            */
            //return JsonUtility.FromJson<T>(str);
            //return SimpleJson.SimpleJson.DeserializeObject<T>(str);
            return JsonMapper.ToObject<T>(str);
        }

        public static object FromJson(Type type, string str) {
            /*
            object t = MongoHelper.FromJson(type, str);
            ISupportInitialize iSupportInitialize = t as ISupportInitialize;
            if (iSupportInitialize == null) {
                return t;
            }
            iSupportInitialize.EndInit();
            return t;
            */
            //return JsonUtility.FromJson(str, type);
            //return SimpleJson.SimpleJson.DeserializeObject(str, type);
            return JsonMapper.ToObject(type, str);
        }

        public static T Clone<T>(T t) {
            return FromJson<T>(ToJson(t));
        }
    }
}